<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2024/1/18
 * Time: 16:36
 *describe:****
 */

namespace app\api\controller;


use addons\faqueue\library\QueueApi;
use app\common\controller\Api;
use app\common\library\Token;
use app\common\model\GoodsModel;
use think\Db;

class Goods extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = '*';


    /**
     * 商品列表
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/18 21:44
     */
    public function lists()
    {
        $params = $this->request->param();
        $where = [];
        if (isset($params['title']) && !empty($params['title'])) {
            $title = htmlspecialchars($params['title']);
            $where = ['title' => ['like', '%' . $title . '%'],];
        }
//        else {
//            if (!isset($params['category_id']) || $params['category_id'] == 0) {
//                $this->error("分类id不存在");
//            }
//        }
        $order = [];
        if (isset($params['price']) && !empty($params['price'])) {
            $order['price'] = $params['price'];
        }
        if (isset($params['sales']) && !empty($params['sales'])) {
            $order['totalSales'] = $params['sales'];
        }
        if (isset($params['createtime']) && !empty($params['createtime'])) {
            $order['createtime'] = $params['createtime'];
        }
        $where['deletetime'] = 0;
        $field = 'id,title,category_ids,image,images,content,price,likes,views,sales,status,createtime,updatetime,is_hot,goods_desc,virtually_nums,weigh,virtually_nums+sales totalSales';
        $category_id = $params['category_id'] ?? 0;
        $order['weigh'] = 'desc';
        $domain= $this->request->domain();
        $lists = Db::table('zwa_goods')
            ->field($field)
            ->where(function ($query) use ($category_id) {
                if ($category_id) {
                    return $query->where(['category_ids'=>$category_id]);
                } else {
                    return $query;
                }
            })->where($where)->order($order)
//            ->fetchSql(true)->select();

            ->paginate($this->request->param('list_rows', 10));
        $lists->each(function ($item) use($domain){
            $item['images'] = json_decode($item['images'], true);
            foreach ($item['images'] as $k=>$image){
                $item['images'][$k]= $domain.$image;
            }
            return $item;
        });


        $this->success("", $lists);
//        return QueueApi::push('addons\faqueue\library\jobs\EmailJob', ['id'=>454545]);
    }

    public function detail()
    {
        $params = $this->request->param();

        if (empty($params['id'])) {
            $this->error("商品id不存在");
        }

        $goodsModel = new GoodsModel();
        $goods = $goodsModel->where(['id' => $params['id']])->find();
        if(!$goods){
            $this->error('商品不存在');
        }
        $token = request()->param('token');
        $goodsLike=null;
        if($token){
            $result=Token::get($token);
            if($result){
                $this->error(__('Please login first'), null, 401);
            }
            $goodsLike= Db::name('goods_likes')->where(['uid'=>$result['user_id'],'goods_id'=>$params['id']])->find();
        }
        $domain= $this->request->domain();
        $goods['like_action'] =  $goodsLike ?1 :0;
        $goods['images'] = json_decode($goods['images'], true);
        $images=[];
        foreach ($goods['images'] as $k=>$image){
//            $image=str_replace($domain,'',$image);
            $images[]= $domain.$image;
        }
        $goods['images']= $images;
        if($goods){
            $goodsModel->where(['id'=>$params['id']])->update(['views'=>$goods['views']+1,'updatetime'=>time()]);
        }

        $this->success("", $goods);

    }



    /**环境分类商品
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/24 15:36
     */
    public  function  typeGoods(){

        $params = $this->request->param();
        $where = [];
        if (isset($params['title']) && !empty($params['title'])) {
            $title = htmlspecialchars($params['title']);
            $where = ['title' => ['like', '%' . $title . '%'],];
        }
        $type= $params['type'] ?? 1;
        $category_id= Db::name('product_category')->where(['type'=>$type])->order(['id'=>'desc'])->column('id');

        $order = [];
        if (isset($params['price']) && !empty($params['price'])) {
            $order['price'] = $params['price'];
        }
        if (isset($params['sales']) && !empty($params['sales'])) {
            $order['totalSales'] = $params['sales'];
        }
        if (isset($params['createtime']) && !empty($params['createtime'])) {
            $order['createtime'] = $params['createtime'];
        }
        $where['deletetime'] = 0;
        $field = 'id,title,category_ids,image,images,content,price,likes,views,sales,status,createtime,updatetime,is_hot,goods_desc,virtually_nums,weigh,virtually_nums+sales totalSales';
//        $category_id = $params['category_id'] ?? 0;
        $order['weigh'] = 'desc';
        $lists = Db::table('zwa_goods')
            ->field($field)
            ->whereIn('category_ids',$category_id)->where($where)->order($order)
//            ->fetchSql(true)->select();
//        var_dump($lists);die;
            ->paginate($this->request->param('list_rows', 10));
        $this->success("", $lists);

    }


}
